package com.authine.cloudpivot.ext.service.impl;

import com.authine.cloudpivot.engine.api.facade.OrganizationFacade;
import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.ext.domain.NumberVo;
import com.authine.cloudpivot.ext.domain.WgkVo;
import com.authine.cloudpivot.ext.mapper.NumberMapper;
import com.authine.cloudpivot.ext.mapper.WgkMapper;
import com.authine.cloudpivot.ext.service.CodeService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author liujing
 * @version 1.0.0
 * @ClassName CodeServiceImpl.java
 * @Description
 * @Date: 2021/6/10
 */
@Service
@Slf4j
public class CodeServiceImpl implements CodeService {

    @Autowired
    private WgkMapper wgkMapper;
    @Autowired
    private NumberMapper numberMapper;
    private static final String creater = "2c9280a26706a73a016706a93ccf002b";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int getCode(String corpId) {
        NumberVo vo = numberMapper.getMaxCode(corpId);
        if (null==vo){
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            int i = numberMapper.insertCode(1,corpId,id,creater);
            log.info(corpId+"首次插入number表中数据 id = "+id+",新增数据条数 i = "+i);
            return 1;
        }
        int i = numberMapper.update(vo.getCode().add(new BigDecimal("1")),vo.getId());
        log.info(corpId+"本次更新number表中数据 id = "+vo.getId()+",code = "+vo.getCode().intValue()+1+"更新数据条数 i = "+i);
        return vo.getCode().intValue()+1;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateData(OrganizationFacade organizationFacade) {
        List<String> creaters = wgkMapper.getCreaters();
        for (String userId:creaters){
            updateWgkCode(userId);
            String cropId = "";
            int count = wgkMapper.getCount(userId);
            UserModel user = organizationFacade.getUser(userId);
            if (null != user){
                List<DepartmentModel> parentDepartments = organizationFacade.getParentDepartments(user.getDepartmentId());
                Collections.reverse(parentDepartments);
                for (DepartmentModel departmentModel:parentDepartments){
                    if ("village".equals(departmentModel.getExtend3())){
                        cropId = departmentModel.getId();
                    }
                }
                numberMapper.insertCode(count==0?1:count,cropId,UUID.randomUUID().toString().replaceAll("-", ""),creater);
            }
        }
    }
    /**
     * @author liujing
     * @description: 批量更新务工表人员编号条数
     * @date 2021/6/11
     * @param userId
     * @return void
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateWgkCode(String userId){
        List<WgkVo> list = Lists.newArrayList();
        List<String> wgkIds = wgkMapper.getWgkIds(userId);
        if (CollectionUtils.isNotEmpty(wgkIds)){
            for (int i = 0;i<wgkIds.size();i++){
                WgkVo vo = new WgkVo();
                vo.setId(wgkIds.get(i));
                vo.setCode(i+1);
                list.add(vo);
            }
        }
        int i = wgkMapper.updateCode(list);
        log.info("批量更新务工表人员编号条数{}",i);
    }
}
